﻿using Inaction.ServiceContract;
using Inaction.Rule;
using System;
using System.ComponentModel;

namespace Inaction
{
    [Serializable]
    public class AppConfig
    {
        #region 属性
        string crypKey = "Inaction";
        public string CrypKey
        {
            get { return crypKey; }
            set { crypKey = value; }
        }

        RunLocation runLocation = RunLocation.Local;
        [DisplayName("运行位置")]
        public RunLocation RunLocation
        {
            get { return runLocation; }
            set { runLocation = value; }
        }
        [DisplayName("应用名称")]
        public string ApplicationName { get; set; }

        private string startUpView =
#if WINDOWS
            "Inaction.UI.Main;Inaction.UI.Main.MainForm2";
#elif WPF
            "pack://application:,,,/Inaction.UI.Main;component/MainWindow.xaml";
#else 
            string.Empty;
#endif
        [DisplayName("客户端主界面")]
        public string StartUpView
        {
            get { return startUpView; }
            set { startUpView = value; }
        }

        [DisplayName("应用类型")]
        public AppType AppType { get; set; }

        [DisplayName("服务类型")]
        public ServiceType ServiceType { get; set; }

        [DisplayName("服务地址")]
        public string ServerUrl { get; set; }
        #endregion

        #region 服务设置
        [DisplayName("数据库类型")]
        public SqlType SqlType { get; set; }
        [DisplayName("数据库连接串")]
        public string ConnectionString { get; set; }
        #endregion

        static string ConfigFile
        {
            get
            {
                return @"Config\Application.xml";
            }
        }

        static AppConfig instance = null;
        public static AppConfig Instance
        {
            get
            {
                if (instance == null)
                {
                    try
                    {
                        var s = new System.Xml.Serialization.XmlSerializer(typeof(AppConfig));
                        using (var fs = new System.IO.FileStream(ConfigFile, System.IO.FileMode.Open))
                        {
                            instance = (AppConfig)s.Deserialize(fs);
                            if (instance.RunLocation != RunLocation.Client)
                            {
                                instance.ConnectionString = Security.DEncrypt.RC2DecryptStr(instance.ConnectionString, "Inaction");
                            }
                            fs.Close();
                            fs.Dispose();
                        }
                    }
                    catch
                    {
                        instance = new AppConfig()
                        {
                            RunLocation = Inaction.RunLocation.Local,
                            ServiceType = ServiceType.Local
                        };
                    }
                }
                return instance;
            }
        }

        private AppConfig() { }
        public void Save()
        {
            var s = ServiceContainer.GetService<ISerializerHelper>().GetSerializer(SerializeType.Xml);
            if (Instance.RunLocation != Inaction.RunLocation.Client)
            {
                instance.ConnectionString = Security.DEncrypt.RC2EncryptStr(instance.ConnectionString, "Inaction");
            }
            s.SerializeToFile(instance, ConfigFile);
            instance = null;
            Inaction.DbProvider.Instance = null;
            Inaction.Mapping.ClassInfo.ClearCache();
        }
    }

    public enum AppType
    {
        Web,
        WinForm,
        WPF
    }

    public enum RunLocation
    {
        Local,
        Client,
        Server
    }
}
